<?php
class SiteUser extends WebUser
{
    const USERNAME_PREFIX = 'acc_';
    const VISITOR_COOKIE_NAME = 'isvisitor';
    const VISITOR_UID_COOKIE_INDEX = 0;

    const REMEMBER_ME_PARAM   = 'remember';
    const PASSWORD_STATUS_KEY = 'password_sent';

    private $uidCookie;

    protected  $strategy;

    public static function GetUsernameByAccountId($accountId)
    {
        return self::USERNAME_PREFIX . $accountId;
    }

    public function LoginById( $accountId, $remember = false )
    {
        $this->Login( self::GetUsernameByAccountId( $accountId ), $remember );
    }

    public function Login( $username, $remember = false )
    {
        parent::Login($username, $remember);

        $this->strategy->SetLoginCookie($this);
    }

    public function Logout()
    {
        parent::Logout();

        $this->strategy->SetLogoutCookie();
    }

    public function Initialize()
    {
        $this->isRememberMeEnabled = true;

        $this->InitStrategy();

        parent::Initialize();
    }

    protected function InitStrategy()
    {
        $this->strategy = new NoCacheCookieStrategy();
    }

    public function GetAccountId()
    {
        return substr($this->GetUsername(), strlen(self::USERNAME_PREFIX));
    }

    public function GetUid()
    {
        if ( !isset($this->uidCookie) )
        {
            $this->uidCookie = new Cookie(self::VISITOR_COOKIE_NAME);
        }
        return $this->uidCookie->GetValue(self::VISITOR_UID_COOKIE_INDEX);
    }

    public function GetLoginUrl()
    {
        return '/';
    }

    public function PrepareLogin()
    {
        Session::SetValue('redirect_url', Request::GetURL());

        $this->strategy->SetPreLoginCookie();
    }

    public function OnParseLoginPage()
    {
        $this->strategy->RemoveLoginRequiredCookie($this);
    }

    public function OnUpdateCart()
    {
        $this->strategy->UpdateCartCookie($this);
    }

    public function ReviveCookies()
    {
        $this->strategy->ReviveCookies();
    }
}